import { Client } from "../client/Client";
import { User } from "./User";
import { MessageDto } from "./dto/Message";
import { MessageAttachmentDto } from "./dto/MessageAttachment";
import { MessageEmbedDto } from "./dto/MessageEmbed";
import { Snowflake } from "./utils/Snowflake";

export class Message {
    
    id!: Snowflake
    channel_id!: Snowflake
    guild_id?: Snowflake
    content!: string
    timestamp!: string
    timestamp_number!: number
    authod_id!: Snowflake
    edited_timestamp?: string | null
    embeds!: MessageEmbedDto[]
    attachments!: MessageAttachmentDto[]

    constructor(private client: Client, dto: MessageDto) {
        this._patch(dto);
    }

    //author: UserDto

    get author() {
        return this.client.users[this.authod_id];
    }

    _patch(data: MessageDto) {
        this.id = data.id;

        this.channel_id = data.channel_id;
        this.guild_id = data.guild_id;
        this.content = data.content;
        this.timestamp = data.timestamp;
        this.authod_id = data.author.id;
        this.edited_timestamp = data.edited_timestamp;
        this.attachments = data.attachments;

        const { timestamp } = Snowflake.deconstruct(data.id)

        this.timestamp_number = timestamp

        if (this.client.users[data.author.id]) {
            this.client.users[data.author.id]._patch(data.author)
        } else {
            this.client.users[data.author.id] = new User(this.client, data.author)
        }
    }

}
